package net.maku.events.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import net.maku.events.entity.ActivityBriskWalkEntity;
import net.maku.events.entity.ActivityKnowledgeContestEntity;
import net.maku.events.vo.ActivityTopVO;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.events.convert.ActivitySnapshotConvert;
import net.maku.events.entity.ActivitySnapshotEntity;
import net.maku.events.query.ActivitySnapshotQuery;
import net.maku.events.vo.ActivitySnapshotVO;
import net.maku.events.dao.ActivitySnapshotDao;
import net.maku.events.service.ActivitySnapshotService;
import net.maku.framework.security.user.SecurityUser;
import net.maku.system.service.SysOrgService;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 随手拍活动
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-03-22
 */
@Service
@AllArgsConstructor
public class ActivitySnapshotServiceImpl extends BaseServiceImpl<ActivitySnapshotDao, ActivitySnapshotEntity> implements ActivitySnapshotService {

    private final SysOrgService sysOrgService;

    @Override
    public PageResult<ActivitySnapshotVO> page(ActivitySnapshotQuery query) {
        IPage<ActivitySnapshotEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));
        List<ActivitySnapshotVO> result = BeanUtil.copyToList(page.getRecords(), ActivitySnapshotVO.class);
        return new PageResult<>(result, page.getTotal());
    }

    private LambdaQueryWrapper<ActivitySnapshotEntity> getWrapper(ActivitySnapshotQuery query) {
        LambdaQueryWrapper<ActivitySnapshotEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotEmpty(query.getActivityTitle()), ActivitySnapshotEntity::getActivityTitle, query.getActivityTitle())
                .eq(ObjectUtils.isNotEmpty(query.getStatus()), ActivitySnapshotEntity::getActivityStatus, query.getStatus())
                .eq(ObjectUtils.isNotEmpty(query.getActivityStatus()), ActivitySnapshotEntity::getActivityStatus, query.getActivityStatus())
                .eq(ObjectUtils.isNotEmpty(query.getWhetherTop()), ActivitySnapshotEntity::getWhetherTop, query.getWhetherTop())
                .orderByDesc(ActivitySnapshotEntity::getId);
        dataScopeWrapper(wrapper);
        return wrapper;
    }

    @Override
    public void save(ActivitySnapshotVO vo) {
        sysOrgService.setActivityJoinOrgName(vo);
        ActivitySnapshotEntity entity = BeanUtil.copyProperties(vo, ActivitySnapshotEntity.class);
        entity.setOrgId(SecurityUser.getUser().getOrgId());
        baseMapper.insert(entity);
    }

    @Override
    public void update(ActivitySnapshotVO vo) {
        sysOrgService.setActivityJoinOrgName(vo);
        ActivitySnapshotEntity entity = BeanUtil.copyProperties(vo, ActivitySnapshotEntity.class);
        updateById(entity);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        ActivitySnapshotEntity entity = new ActivitySnapshotEntity();
        entity.setId(id);
        entity.setStatus(status);
        baseMapper.updateById(entity);
    }

    @Override
    public void whetherTop(ActivityTopVO activityTopVO) {
        ActivitySnapshotEntity entity = new ActivitySnapshotEntity();
        entity.setId(activityTopVO.getActivityId());
        entity.setWhetherTop(activityTopVO.getStatus());
        entity.setTopImg(activityTopVO.getTopImg());
        baseMapper.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}